package com.sike.netty.jinjie.codec.protocol.encoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class JsonStringToByteEncoder extends MessageToByteEncoder<String> {

    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
        //1. 4 字节的魔数
        out.writeBytes(new byte[]{1,3,1,4});

        //2. 1 字节的版本协议号
        out.writeByte(1);

        byte[] bytes = msg.getBytes();

        //3 4 字节的消息长度
        out.writeInt(bytes.length);

        //4 消息内容
        out.writeBytes(bytes);
    }
}
